var timer_refresh_img = null;
var image_canvas = document.getElementById("image_canvas");
console.log(image_canvas,"pl")
var image_canvas_ctx = image_canvas.getContext('2d');

// 示例开始，以下几行代码即可成功入会专属云平台:
var inMeeting = false
window.onload = function () {
  getDemoConfig()
  window.zjsdk3 = new ZjSdk3();
  // 重写回调
  zjsdk3.on_dev_call_succeed = demo_on_dev_call_succeed
}

window.image_select = function () { // 本代码演示了如何将本地图片做为双流源共享到会议中(本地文件->canvas->mediastream->发送远端)
  var image = new Image();
  var freader = new FileReader();
  freader.onload = () => {
    if (timer_refresh_img) {
      clearInterval(timer_refresh_img);
      timer_refresh_img = null;
      image.src = '';
    }

    timer_refresh_img = setInterval(() => {
      image.src = freader.result;
      image_canvas_ctx.drawImage(image, 0, 0, 960, 540); // 由于发给无端的流是从canvas抓出来的,因此canvas应该使用1920*1080才合理
    }, 300)

    window.setPropForVideo("image_video", image_canvas.captureStream(5));
  }

  var jpg_selector = document.getElementById("jpg_selector").files[0];
  freader.readAsDataURL(jpg_selector);
}

// 呼叫成功时的回调
function demo_on_dev_call_succeed() {
  //回显本地视频
  zjsdk3.local_dev_show_myvideo(['local_video'], true)
  // 显示远端视频
  zjsdk3.show_video_by_orders(['farend_video', 'farend_video2', 'farend_video3', 'farend_video4', 'farend_video5'])
}

// 呼入会议
window.start_call = async function () {
  if (!inMeeting) {
    inMeeting = true
    // 将用户输入的服务器地址、会议号、入会密码、昵称存入配置文件中
    setDemoConfig()
    await zjsdk3.make_call_now(demo_config.server.ip, demo_config.server.port, demo_config.meeting.cuid, demo_config.meeting.joinpwd, demo_config.meeting.nick_name, demo_config.meeting.account)
  }
};

// 离开会议室
window.disconnect = async function () {
  await zjsdk3.disconnect_now()
  window.inMeeting = false
};

// 共享屏幕
window.Share_screen = function () {
  zjsdk3.share_screen_start()
}
// 结束共享
window.End_sharing = function () {
  zjsdk3.share_xxx_stop()
}

// 共享图片
window.image_share = async function () {
  document.getElementById('whiteboard').hidden = true;
  document.getElementById('image_canvas').hidden = false;
  let stream = document.getElementById('jpg_selector').files[0]
  if (stream) {
    let imgList = []
    imgList.push(stream)
    let elemId = 'image_video'
    await zjsdk3.share_img_start(imgList, null, elemId)
  }
}

// 结束图片共享
window.image_sharing = function () {
  document.getElementById('image_canvas').hidden = true;
  document.getElementById('image_video').hidden = true;
  zjsdk3.share_xxx_stop()
}

// 共享视频
window.video_share = function () {
  var videoStream = document.querySelector('#video_selector').files[0]
  var videoSrc = URL.createObjectURL(videoStream); //通过URL.createObjectURL(blob)可以获取当前文件的一个内存URL
  var video = document.getElementById("enjoy_video");
  video.src = videoSrc
  video.addEventListener('canplay', async function () { //canplay提示该视频已准备好开始播放
    var stream = video.captureStream(); //使用captureStream生成MediaStream
    await zjsdk3.share_stream_start(stream);
    video.play();
  })
}

//结束共享
window.video_sharing = function () {
  var video = document.getElementById("enjoy_video");
  video.src = ''
  zjsdk3.share_xxx_stop()
}

// 共享PDF
window.pdf_share = function () {
  document.getElementById('whiteboard').hidden = false;
  document.getElementById('image_canvas').hidden = true;
  let pdf_file = document.getElementById("pdf_selector").files[0];
  zjsdk3.share_pdf_start(pdf_file, null, 'whiteboard')
}

// 取消共享PDF
window.PDF_sharing = function () {
  zjsdk3.share_xxx_stop()
  document.getElementById('whiteboard').hidden = true;
}

// PDF翻页（下一页）
window.pdf_nextpage = function () {
  zjsdk3.share_pdf_next_page()
}

// PDF翻页（上一页）
window.pdf_previouspage = function () {
  zjsdk3.share_pdf_privious_page()
}

// 共享白板
window.open_whiteboard = async function () {
  document.getElementById('whiteboard').hidden = false;
  await zjsdk3.share_whiteboard_start('whiteboard')
}

// 开启批注
window.whiteboard_pizhui = function () {
  zjsdk3.share_whiteboard_use_pen(5, '#000');
}


// 关闭白板
window.whiteboard_stop = function () {
  zjsdk3.share_xxx_stop()
  document.getElementById('whiteboard').hidden = true;
}

// 修改笔画的粗细,颜色
window.whiteboard_penwidth = function () {
  zjsdk3.share_whiteboard_use_pen(Math.floor(Math.random() * 20), '#' + Math.random().toString(16).substr(2, 6));
}

// 删除白板上的笔画
window.whiteboard_erase = function () {
  zjsdk3.share_whiteboard_use_rubber()
}

//清除自己的批注
window.whiteboard_oneself = function () {
  zjsdk3.share_whiteboard_clear_self_path()
}

//清除所有人的批注
window.whiteboard_allsomeone = function () {
  zjsdk3.share_whiteboard_clear_all_path()
}

//清除其他人的批注
window.whiteboard_other = function () {
  zjsdk3.share_whiteboard_clear_other_path()
}

//禁止其他人批注
window.whiteboard_Prohibit = function () {
  zjsdk3.share_whiteboard_denyshare()
}